package com.yc.controller.system.doctor;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yc.controller.base.BaseController;
import com.yc.entity.Page;
import com.yc.entity.system.User;
import com.yc.service.system.doctor.IDoctorAdditionService;
import com.yc.service.system.doctor.IDoctorBCBookingService;
import com.yc.service.system.doctor.IDoctorManager;
import com.yc.util.Const;
import com.yc.util.Jurisdiction;
import com.yc.util.PageData;



@Controller
@RequestMapping(value="/doctor")
public class DoctorController extends BaseController {
	
	String menuUrl = "doctor/publishadditionList.do"; //菜单地址(权限用)
	
	
	@Resource
	private IDoctorManager doctorManager;
	
	@Resource
	private IDoctorAdditionService doctorAdditionService;
	
	
	@Resource
	private IDoctorBCBookingService doctorBCBookingService;
	
	
	@RequestMapping("/getlist")
	public ModelAndView getlist(Page page) throws Exception{
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(null != keywords && !"".equals(keywords)){
			pd.put("keywords", keywords.trim());
		}
		
		page.setPd(pd);
		List<PageData> list = doctorManager.getDoctorList(page);
		

		mv.setViewName("system/hospital_list");
		mv.addObject("hospitalList", list);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
		
		
		
	}
	
	@RequestMapping("/publishadditionList")
	public ModelAndView publishaddition(Page page,HttpSession session) throws Exception{
		
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(null != keywords && !"".equals(keywords)){
			pd.put("keywords", keywords.trim());
		}
		
		//只拿医生本人的数据
		User user = (User) session.getAttribute(Const.SESSION_USER);
		pd.put("USER_ID", user.getUSER_ID());
		PageData item = doctorManager.findByUserId(pd);
		
		if(item!=null)
		pd.put("DOCTOR_ID", item.getString("DOCTOR_ID"));
		
		page.setPd(pd);
		List<PageData> list = doctorAdditionService.getDoctorAdditionList(page);
		
		mv.setViewName("system/doctor/doctoraddition_list");
		mv.addObject("doctorAdditionList", list);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
		
	}
	

	
	@RequestMapping("/bcBooking")
	public ModelAndView bcBooking(Page page,HttpSession session) throws Exception{
		
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		String keywords = pd.getString("keywords");				//关键词检索条件
		if(null != keywords && !"".equals(keywords)){
			pd.put("keywords", keywords.trim());
		}
		
		//只拿医生本人的数据
		User user = (User) session.getAttribute(Const.SESSION_USER);
		pd.put("USER_ID", user.getUSER_ID());
		PageData item = doctorManager.findByUserId(pd);
		
		if(item!=null)
		pd.put("DOCTOR_ID", item.getString("DOCTOR_ID"));
		
		page.setPd(pd);
		List<PageData> list = doctorBCBookingService.getBCBookingist(page);
		
		mv.setViewName("system/doctor/doctor_bc_publish_list");
		mv.addObject("doctorBCList", list);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
		
	}
	
	
	/**删除
	 * @param out
	 */
	@RequestMapping(value="/deleteaddition")
	public void deleteaddition(PrintWriter out) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"删除addition");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		pd = this.getPageData();
		doctorAdditionService.delete(pd);
		out.write("success");
		out.close();
	}
	
	/**删除
	 * @param out
	 */
	@RequestMapping(value="/deleteBCBooking")
	public void deleteBCBooking(PrintWriter out) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"删除BC");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		pd = this.getPageData();
		doctorBCBookingService.delete(pd);
		out.write("success");
		out.close();
	}
	
	
	
	@RequestMapping(value="/goAddBCBooking")
	public ModelAndView goAddBCBooking(HttpSession session) throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();

		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		pd.put("USER_ID", user.getUSER_ID());

		PageData item = doctorManager.findByUserId(pd);
		
		
		mv.setViewName("system/doctor/doctor_bc_publish_edit");
		mv.addObject("action", "saveBCBooking");
		mv.addObject("item",item);

		return mv;
		
	}
	
	@RequestMapping(value="/saveBCBooking")
	public ModelAndView saveBCBooking(HttpSession session) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"新增BCBooking");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		pd.put("USER_ID", user.getUSER_ID());

		PageData item = doctorManager.findByUserId(pd);
		
		pd.put("TID", this.get32UUID());	//主键
		pd.put("RCDSTS", "A");
		pd.put("DOCTOR_ID", item.getString("DOCTOR_ID"));
	
		doctorBCBookingService.save(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	@RequestMapping("/goEditBCBooking")
	public ModelAndView goEditBCBooking() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		
		
		PageData item = doctorBCBookingService.findById(pd);
		
		mv.setViewName("system/doctor/doctor_bc_publish_edit");
		mv.addObject("item", item);
		mv.addObject("action", "editBCBooking");
		mv.addObject("pd", pd);
		
		return mv;
		
	}
	
	
	@RequestMapping("/editBCBooking")
	public ModelAndView editBCBooking() throws Exception{
		
		logBefore(logger, Jurisdiction.getUsername()+"修改BCBooking");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		pd.put("RCDSTS", "A");
		
		doctorBCBookingService.edit(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
		
	}
	
	@RequestMapping(value="/goAddAddition")
	public ModelAndView goAddHospital(HttpSession session) throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();

		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		pd.put("USER_ID", user.getUSER_ID());

		PageData item = doctorManager.findByUserId(pd);
		
		
		mv.setViewName("system/doctor/doctoraddition_edit");
		mv.addObject("action", "saveAddition");
		mv.addObject("item",item);

		return mv;
		
	}
	
	@RequestMapping(value="/saveAddition")
	public ModelAndView saveAddition(HttpSession session) throws Exception{
		logBefore(logger, Jurisdiction.getUsername()+"新增Addition");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		User user = (User) session.getAttribute(Const.SESSION_USER);
		
		pd.put("USER_ID", user.getUSER_ID());

		PageData item = doctorManager.findByUserId(pd);
		
		pd.put("TID", this.get32UUID());	//主键
		pd.put("RCDSTS", "A");
		pd.put("DOCTOR_ID", item.getString("DOCTOR_ID"));
	
		doctorAdditionService.save(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	@RequestMapping("/goEditAddition")
	public ModelAndView goEditAddition() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		
		
		PageData item = doctorAdditionService.findById(pd);
		
		mv.setViewName("system/doctor/doctoraddition_edit");
		mv.addObject("item", item);
		mv.addObject("action", "editAddition");
		mv.addObject("pd", pd);
		
		return mv;
		
	}
	
	@RequestMapping("/editAddition")
	public ModelAndView editAddition() throws Exception{
		
		logBefore(logger, Jurisdiction.getUsername()+"修改Addition");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		pd.put("RCDSTS", "A");
		
		doctorAdditionService.edit(pd);
		
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
		
	}
	
	
	
	@RequestMapping("/viewBCBooking")
	public ModelAndView viewBCBooking() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		
		
		PageData item = doctorAdditionService.findById(pd);
		
		mv.setViewName("system/doctor/doctor_bc_publish_view");
		mv.addObject("item", item);
		mv.addObject("pd", pd);
	
		return mv;
		
	}
	@RequestMapping("/viewAddition")
	public ModelAndView viewAddition() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
	
		
		
		PageData item = doctorAdditionService.findById(pd);
		
		mv.setViewName("system/doctor/doctoraddition_view");
		mv.addObject("item", item);
		mv.addObject("pd", pd);
	
		return mv;
		
	}
	
	
	
	
	
	
	
	
	

}
